package com.dishdex.menuhub.module.service;

import com.dishdex.menuhub.module.entity.${entity};
import com.dishdex.menuhub.module.mapper.${entity}Mapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class I${entity}Service{

    @Resource
    private ${entity}Mapper ${entity?uncap_first}Mapper;

    public ${entity} get${entity}ById(Long id){
        if(id == null){
            throw new RuntimeException("id不能为空");
}
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}Mapper.get${entity}ById(id);
        if(${entity?uncap_first} == null){
            throw new RuntimeException("查询的${entity}不存在");
}
        return ${entity?uncap_first};
}

    public ${entity} extract${entity}ById(Long id){
        if(id == null){
            throw new RuntimeException("id不能为空");
}
        ${entity} ${entity?uncap_first} = ${entity?uncap_first}Mapper.extract${entity}ById(id);
        if(${entity?uncap_first}==null){
            throw new RuntimeException("查询的${entity}不存在");
}
        return ${entity?uncap_first};
}

    public Long insert${entity}(
    <#assign filteredFields = table.fields?filter(f ->
            f.columnName != "id"
            && f.columnName != "create_time"
            && f.columnName != "update_time"
            && f.columnName != "is_deleted")>
    <#list filteredFields as field>
        ${field.propertyType} ${field.propertyName}<#sep>,
    </#list>
) {
        <#list filteredFields as field>
            <#if field.propertyType == "String">
                if (${field.propertyName} == null || ${field.propertyName}.trim().isEmpty()) {
                throw new RuntimeException("${field.propertyName}不能为空");
                }
            <#elseif !field.propertyType?matches("^(byte|short|int|long|float|double|boolean|char)$")>
                if (${field.propertyName} == null) {
                throw new RuntimeException("${field.propertyName}不能为空");
                }
            </#if>
        </#list>

        ${entity} ${entity?uncap_first} = new ${entity}();
        int temp = (int)(System.currentTimeMillis()/1000);
        <#list filteredFields as field>
            <#if field.propertyType =="String">
                ${entity?uncap_first}.set${field.propertyName?cap_first}(${field.propertyName}.trim());
                <#else>
                    ${entity?uncap_first}.set${field.propertyName?cap_first}(${field.propertyName});
            </#if>
        </#list>
        ${entity?uncap_first}.setCreateTime(temp);
        ${entity?uncap_first}.setUpdateTime(temp);
        ${entity?uncap_first}.setIsDeleted((byte)0);
        ${entity?uncap_first}Mapper.insert${entity}(${entity?uncap_first});
        return ${entity?uncap_first}.getId();
}

        public Long update${entity}(
        <#assign filteredFields = table.fields?filter(f ->
        f.columnName != "create_time"
        && f.columnName != "update_time"
        && f.columnName != "is_deleted")>
        <#list filteredFields as field>
            ${field.propertyType} ${field.propertyName}<#sep>,
        </#list>
        ) {
        <#list filteredFields as field>
            <#if field.propertyType == "String">
                if (${field.propertyName} == null || ${field.propertyName}.trim().isEmpty()) {
                throw new RuntimeException("${field.propertyName}不能为空");
                }
            <#elseif !field.propertyType?matches("^(byte|short|int|long|float|double|boolean|char)$")>
                if (${field.propertyName} == null) {
                throw new RuntimeException("${field.propertyName}不能为空");
                }
            </#if>
        </#list>
        if(${entity?uncap_first}Mapper.get${entity}ById(id) ==null){
            throw new RuntimeException("更新的${entity}不存在");
}
        ${entity} ${entity?uncap_first} = new ${entity}();
        int temp = (int)(System.currentTimeMillis()/1000);
        <#list filteredFields as field>
            <#if field.propertyType =="String">
                ${entity?uncap_first}.set${field.propertyName?cap_first}(${field.propertyName}.trim());
            <#else>
                ${entity?uncap_first}.set${field.propertyName?cap_first}(${field.propertyName});
            </#if>
        </#list>
        ${entity?uncap_first}.setUpdateTime(temp);
        ${entity?uncap_first}Mapper.update${entity}(${entity?uncap_first});
        return ${entity?uncap_first}.getId();
        }

        public int delete${entity}(Long id){
        if(id == null){
            throw new RuntimeException("id不能为空");
}
        if(${entity?uncap_first}Mapper.get${entity}ById(id) == null){
            throw new RuntimeException("要删除的${entity}不存在");
}
        return ${entity?uncap_first}Mapper.delete${entity}(id);
}

        public List<${entity}> getAll${entity}(){
            List<${entity}> l1 = ${entity?uncap_first}Mapper.getAll${entity}();
            if(l1 == null){
                return new ArrayList<>();
}
            return l1;
}
}